import axios from 'axios';


async function authorization(){
    const storage = localStorage;
    let hasLogin = false;
    let username = storage.getItem('username.myblog');

    const expiredTime = Number(storage.getItem('expiredTime.myblog'));
    const current = (new Date()).getTime();
    const refreshToken = storage.getItem('refresh.myblog');

    // 初始 token 未过期
    if(expiredTime > current){
        hasLogin = true;
        console.log('authorization access')
    }

    // 初始 token 过期
    // 申请刷新 token
    else if(refreshToken !== null){
        try{
            let response = await axios.post('/api/token/refresh/', {refresh: refreshToken});
           // Date.parse(...) 返回1970年1月1日UTC以来的毫秒数
            // Token 被设置为1分钟，因此这里加上60000毫秒
            const nextExpiredTime = Date.parse(response.headers.date) + 60000*60*24;
              // 设置 localStorage
            storage.setItem('access.myblog', response.data.access);
            storage.setItem('expiredTime.myblog', nextExpiredTime);
            storage.setItem('refresh.myblog');
            hasLogin = true;
            console.log('authorization refresh')
        }
        catch(err){
            storage.clear();
            hasLogin = false;
            console.log('authorization err')
        }
    }
    // 无任何有效 token
    else{
        storage.clear();
        hasLogin = false;
        console.log('authorization exp')
    }
    console.log('authorization done')
    return [hasLogin, username]
}
export default authorization;
